package example.springboot.controller;

import example.springboot.common.Result;
import example.springboot.dto.PageResult;
import example.springboot.entity.SystemLog;
import example.springboot.service.SystemLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 *
 * @Author 董熠涛
 * @Date 2025/6/16 20:22
 */
@RestController
@RequestMapping("/api/admin/system-logs")
@CrossOrigin
public class SystemLogController {

    @Autowired
    private SystemLogService systemLogService;

    /**
     * 获取系统日志列表（分页）
     */
    @GetMapping
    public Result<PageResult<SystemLog>> getSystemLogs(
            @RequestParam(required = false) String type,
            @RequestParam(required = false) String date,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size
    ) {
        try {
            PageResult<SystemLog> result = systemLogService.getSystemLogList(type, date, page, size);
            return Result.success(result);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取系统日志失败：" + e.getMessage());
        }
    }

    /**
     * 根据ID获取系统日志详情
     */
    @GetMapping("/{id}")
    public Result<SystemLog> getSystemLogById(@PathVariable Integer id) {
        try {
            SystemLog log = systemLogService.findById(id);
            if (log == null) {
                return Result.error("系统日志不存在");
            }
            return Result.success(log);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取系统日志失败：" + e.getMessage());
        }
    }

    /**
     * 创建系统日志
     */
    @PostMapping
    public Result<SystemLog> createSystemLog(@RequestBody SystemLog log) {
        try {
            SystemLog createdLog = systemLogService.createSystemLog(log);
            return Result.success("创建成功", createdLog);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("创建系统日志失败：" + e.getMessage());
        }
    }

    /**
     * 删除系统日志
     */
    @DeleteMapping("/{id}")
    public Result<String> deleteSystemLog(@PathVariable Integer id) {
        try {
            boolean success = systemLogService.deleteSystemLog(id);
            if (success) {
                return Result.success("删除成功");
            } else {
                return Result.error("删除失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("删除系统日志失败：" + e.getMessage());
        }
    }

    /**
     * 清空所有日志
     */
    @DeleteMapping("/clear")
    public Result<String> clearAllLogs() {
        try {
            boolean success = systemLogService.clearAllLogs();
            if (success) {
                return Result.success("清空成功");
            } else {
                return Result.error("清空失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("清空系统日志失败：" + e.getMessage());
        }
    }
}

